/*if (document.all) { 
docObj = "document.all."
styleObj = ".style"
ie =true
var appVer = navigator.appVersion.toLowerCase();
var iePos  = appVer.indexOf('msie');
//version = parseFloatappVer.substring(iePos+5,appVer.indexOf(';',iePos)));
       //is_major = parseInt(is_minor);
	//alert(is_minor)
}
else
{
docObj = "document."  
styleObj = ""
ie =false
}*/

var ie =document.all;;
//var version;
//var readingid = 0;
var tip;
var ns6;
//var ie;
var showing = false;
var offsetfromcursorX=2;//10; //Customize x offset of tooltip
var offsetfromcursorY=2;//20; //Customize y offset of tooltip

function showTipIE(evt, tip) { 
	tip.style.visibility = 'visible';
	showing = true;
	position(evt, tip, 10, 20);
}

function positiontip(evt){
	if (showing){
		position(evt, tip, 10, 20);
	}
}
function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}
function position(e, popup, offsetfromcursorX, offsetfromcursorY){
		var nondefaultpos=false;
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		//Find out how close the mouse is to the corner of the window
		var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20;
		var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20;
	
		var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX;
		var bottomedge=ie&&!window.opera? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY;
	
		var leftedge=(offsetfromcursorX<0)? offsetfromcursorX*(-1) : -1000;
	
		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<popup.offsetWidth){
		//move the horizontal position of the menu to the left by it's width
			popup.style.left=curX-popup.offsetWidth+"px";
			nondefaultpos=true;
		}
		else if (curX<leftedge)
		{popup.style.left="5px";}
		else{
		//position the horizontal position of the menu where the mouse is positioned
			popup.style.left=curX+offsetfromcursorX+"px";
		//pointerobj.style.left=curX+offsetfromcursorX+"px";
		}
	
		//same concept with the vertical position
		if (bottomedge<popup.offsetHeight){
			popup.style.top=curY-popup.offsetHeight-offsetfromcursorY+"px";
			nondefaultpos=true;
		}
		else{
			popup.style.top=curY+offsetfromcursorY+"px";
		}
}


function hide(){
	tip.style.display = "none";
}

function showtip(evt) { 
	var id = getId(evt);
	tip = document.getElementById('d'+ id);
	showElement(tip, evt);
	showing = true;
}

function showElement(elem, evt){
	var x;
	var y;
	if (ie){
		x = evt.clientX;
		y = evt.clientY;
	}
	else{
		x = evt.pageX;
		y = evt.pageY;
	}
	elem.style.left = x  + 5;
	elem.style.top = y  + 5;
	elem.style.display = "block";
	elem.style.position = "absolute";
}

function saveDefinition(event){
	var sentence;
	if (ie){
		event.scrElement.innerHTML
		sentence = contextMenuTarget.parentNode.innerText;
	}
	else{
		sentence = contextMenuTarget.parentNode.innerHTML.replace(/<[^>]+>/g,"");
	} 
	translation = eval("a" + contextMenuTarget.id);
	readingid = 0;
	language = "none";	

	DefinitionSaver.saveDefintion(readingid, language, contextMenuTarget.parentNode.id, translation, contextMenuTarget.innerHTML, sentence, callBack);

}

function callBack(result)
{ 
	if (!result)
		alert("You must be logged in to save a definition!")
}


var selected = false

function deselectAll() { 
	if (selected){
		selected = false
		deselect(currentWord);
		deselect(previousWord);
		deselect(nextWord);
	}
}
function deselect(elem) { 
	if (elem != null){
		elem.style.fontWeight= "normal";
		elem.style.borderBottom = "none";
		elem.style.fontSize = "100%";
	}
}
function selectPreviousWord(elem){
	elem.style.fontSize = "120%";
	highlightWord(elem);
	previousWord = elem;
}
function selectNextWord(elem){
	elem.style.fontSize = "120%";
	highlightWord(elem);
	nextWord = elem;
}
var nextWord;
var previousWord;
var currentWord;

function selectCurrent(elem){
	elem.style.fontSize = "140%";
	currentWord = elem;
	highlightWord(elem);
}

function highlightWord(elem) { 
	elem.style.borderBottomStyle = "solid"
	elem.style.borderBottomColor = "black"
	elem.style.borderBottomWidth = "1px"
	elem.style.fontWeight= "bold";
}

function getElementId(time) {	
	size = times.length;
	for (i = 0; i < size; i++) {
	    dbl = times[i];
	    if (dbl > time) {
		 	return "w" + i;
	    }
	}
	return "w0";
 }
 
 function getTimeIndex(time) {	
	size = times.length;
	for (i = 0; i < size; i++) {
	    dbl = times[i];
	    if (dbl > time) {
		 	return parseInt(i - 1);
	    }
	}
	return 0;
 }

function  scroll( theElement) {
	  var selectedPosY = 0;
	              
	  while(theElement != null){
	    selectedPosY += theElement.offsetTop;
	    theElement = theElement.offsetParent;
	  }
	      
	var half = parseInt(window.screen.availHeight / 3) - 10;
		
	scrollTo(0,selectedPosY - half)    
  }

var playing = true;
var toPlay = -1;

var wordIndex;
function jumpBack() {
	var time = parent.getTime();
	time = time - 3;
	if (time < 0){
		time = 0;
	}
	if (playing){
		parent.playAt(time);	
	}
	else {
		//parent.setTime(time);
		jumpOccurred = true;
		wordIndex = wordIndex - 3;
		if (wordIndex < 0)
			wordIndex = 0;
		//wordIndex = getTimeIndex(time);
		deselectAll();
		selectById(wordIndex);
	}
}
var jumpOccurred;
function jumpForward() {
	var time = parent.getTime();
	time = time - 3;
	if (time < 0){
		time = 0;
	}
	if (playing){
		parent.playAt(time);	
	}
	else {
		jumpOccurred = true;
		wordIndex = wordIndex + 3;
		//wordIndex = getTimeIndex(time);
		deselectAll();
		selectById(wordIndex);		
	}
}

/*
function setVisibility(){
	var mainPain = document.getElementById("main_pane");
	if (mainPain != null){
		var view = document.getElementById("slide");
		if (playing & hidden){
			mainPain.style.visibility = "hidden";
			mainPain.style.display = "none";
		 	view.style.display = "block";
		}
		else{
			 mainPain.style.display = "block";
		 	mainPain.style.visibility = "visible";	
		 	view.style.display = "none";
		}
	}
}


var hidden = false;

function toggleVisibility(){
	var modalPage =	document.getElementById("modalPage");
	if (hidden){
		modalPage.visibility ="visible";
	}
	else{
		//alert("hide");
		//modalPage.bgColor = "green";
		modalPage.visibility ="hidden";				
	}
}
/*function toggleVisibility(source){
	if (hidden){
		hidden = false;
		setVisibility();
		if (source == 'key')
			parent.document.getElementById("visibility").checked  = false
		window.clearTimeout(slideTimeId)
	}
	else{
		checkSlide()
		slideTimeId = window.setInterval('checkSlide()', 1000);
		hidden = true;
		setVisibility();
		if (source == 'key')
			parent.document.getElementById("visibility").checked  = true
	}
}
*/
function getWordId(evt){
	var id 
	if (!ie){
		id = evt.target.id;
	}
	else{
		id = evt.srcElement.id;
	}

	id = id.substring(1, id.length)
	return parseInt(id);
}

function getId(evt){
	var id;
	if (ie){
		id = evt.srcElement.id;
		if (id.substring(0, 1) == 'w'){
			return evt.srcElement.parentNode.id;
		}
		else{
			return id;
		}
	}
	else{
		id = evt.target.id;
		if (id.substring(0, 1) == 'w'){
			return evt.target.parentNode.id;
		}
		else{
			return id;
		}
		
	}
	
	

}
function move(evt){
	tip.style.left = evt.pageX + 5;
	tip.style.top = evt.pageY + 5;
	tip.style.position= "absolute";
}

function moveIE(evt){
	tip.style.left = evt.clientX + 5;
	tip.style.top = evt.clientY + 5;
	tip.style.position= "absolute";
}


function loaded(){
	//parent.msg("loaded called");
	if (document.getElementsByClassName) {
		var elems = document.getElementsByClassName("linked");
		for (i = 0 ; i < elems.length; i++){
			processLink(elems[i]);
		}
		elems = document.getElementsByClassName("translation");
		for (i = 0 ; i < elems.length; i++){
			processTranslation(elems[i]);
		}
		elems = document.getElementsByClassName("w");
		//parent.msg(elems.length);
		for (i = 0 ; i < elems.length; i++){
			processWord(elems[i]);
		}
	 }
	else {
	  i = 0;
	  var elems = document.getElementsByTagName("span");
	  while (elem = elems[i++]) {
	    if (elem.className == "linked") {
    		  processLinkIE(elem);
		}
		else if (elem.className == "translation") {
			  processTranslationIE(elem, "translation");
		}
		else if (elem.className == "w"){
			processWordIE(elem);
		}
    }
  }
	
	
	
	
	
	//alert("loaded"); 	

	parent.launch();
}

function playWord(evt){
	deselectAll();
	var id = getWordId(evt) + 1;
	play(id);
}

var virgin = true;
function processWord(span){
	if (virgin){
		//alert("process word");
		virgin = false;
	}
	span.onclick = function(event){ playWord(event)};		
}
function processWordIE(span){
	span.ondblclick = function(){ playWord(event)};		
}
function processTranslation(span){
	span.onmouseover = function(event){ showTranslationTip(event)};
	span.onmouseout = function(event){ hide()} ;
}

function processTranslationIE(span){
	span.onmouseover = function(){ showTranslationTip(event)};
	span.onmouseout = function(){ hide()} ;
}

function processLinkIE(span){
	span.onmouseover = function(){ showtip(event)};
	span.onmousemove = function(){ moveIE(event)};	
	span.onmouseout = function(){ hide()} ;
}

function processLink(span){
//	span.onmouseover = function(event){ showtip(event)};
	//span.onmousemove = function(event){ move(event)};	
	//span.onmouseout = function(event){ hide()} ;
	span.addEventListener("touchstart", showtip, false);
	span.addEventListener("touchmove", move, false);
	span.addEventListener("touchend", hide, false);
}

function showTranslationTip(evt) { 
	var id = getId(evt);
	//alert($(id).text());
	tip = document.getElementById('d'+ id);
	var elem = document.getElementById(id);
	tip.style.visibility = "visible";
	tip.style.position = "absolute";

	tip.style.display = "block";
	
		var width;
		if (document.width == null){
			width = document.body.clientWidth
		}
		else width = document.width;
		
		var maxWidth = width- elem.parentNode.offsetLeft;
	
	if (maxWidth > 300){
		if (!ie)
			tip.style.maxWidth = maxWidth;	
		tip.style.left = elem.parentNode.offsetLeft;
		tip.style.top 	= elem.parentNode.offsetTop + elem.parentNode.offsetHeight
	}
	else{
		if (!ie)
			tip.style.maxWidth = width - evt.pageX + 10;
		tip.style.left = evt.pageX + 10; //elem.offsetLeft;
		tip.style.top = evt.pageY + 10;//elem.offsetTop;
	}

	//alert(tip.style.width);
	showing = true;
}

function start() {
	deselectAll();
	if (jumpOccurred){
		jumpOccurred = false;
		toPlay = times[wordIndex];
	}
	playing = true
	//setVisibility()
	// ap = document.getElementById("player") 
	if (toPlay != -1)
		parent.playAt(toPlay)
	else parent.play()
}


function play(value) {
	//if (parent.loaded ){
		//deselectAll()
		if (value == 0){
			parent.playAt(0)
			if (!playing ){
				parent.playset()
			}
		}
		else {	
			toPlay = times[value -1];
			parent.playAt(toPlay)
			if (!playing ){
				parent.playset()
			}
		}
		playing = true

}
function goBack(jumpBack){
	if (playing)
		time = parent.stop();
	else{ 
		parent.playset();
		deselect();
		time = parent.getTime();
	}
	parent.paused = true;
	time = time - jumpBack
	if (time < 0)
		time = 0
	//alert(time);
	parent.playAt(time)	
	parent.paused = false;
}


function pause(time){
	//alert("pause");
	playing = false
	//setVisibility();
	// ap = document.getElementById("player") 

	//msg(time);
	toPlay = -1
	wordIndex = getTimeIndex(time - .05);
	selectById(wordIndex);
	scroll(elem);
}

function selectById(id){
	selected = true;
	elem = document.getElementById("w" + id);
	selectCurrent(elem);
	var previousWordId  = "w" + (id - 1);
	elem = document.getElementById(previousWordId);
	if (elem != null)
		selectPreviousWord(elem);
	var nextWordId  = "w" + (id + 1);
	elem = document.getElementById(nextWordId);
	if (elem != null){
		selectNextWord(elem);
	}
}

function keyListener(e){
	if (ie)
		e = window.event
	keyResponseListener(e)
}

var hideKey = 72; // h key
var spaceKey = 32; // space bar
var backArrowKey = 37; 
var forwardArrowKey = 39;
function keyResponseListener(e){
	value = e.keyCode
	if (value==spaceKey){ 
		parent.tog();
		e.cancelBubble = true;
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
		else{
			return false;
		}
	}
	else if (value==backArrowKey){  
		jumpBack();
		e.cancelButton = true
	}
	else if (value==forwardArrowKey){  
		jumpForward();
		e.cancelButton = true
	}	
	else if (value == hideKey){ 
		parent.reading.toggleVisibility("key");
		return;
	}
	else if(value > 48 & value < 58) { // number keys
		goBack(value - 48);
	}
	/*else if(value==80){
		parent.tog()
	}*/
}




/*var currentSlideIndex = -1;
var slideTimeId = -1;
function checkSlide(){
	var time = parent.getTime();
	var previous = 0;
	for (i = 0; i < slideShowTimes.length; i++){
		var slideTime = slideShowTimes[i];
		if (previous <= time & time < slideTime){
			if (currentSlideIndex != i){
				var view = document.getElementById("slide");
				view.innerHTML = "<img src=\"large_images/" + images[i] + "\" >";
				var height = imageHeight[i];
				view.style.visibility =  "visible";
	 			view.style.display = "block";
				if (ie){
					view.style.top = document.body.clientHeight / 2 - height /2 + "px";
				
				}
				else view.style.top = window.innerHeight / 2 - height /2 + "px";
				currentSlideIndex = i;
			}
		}
		previous = slideTime;
	}	
}*/


var min=8;
var max=30;
function increaseFontSize() {
   var p = document.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {

      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
		if(s!=max) {
        	 s += 3;
	      }
      } else {
         var s = 19;
      }


      p[i].style.fontSize = s+"px"
   }
}
function decreaseFontSize() {
   var p = document.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
		 if(s!=min) {
        	 s -= 3;
      	}
      } else {
         var s = 13;
      }

      p[i].style.fontSize = s+"px"
   }   
}